package com.lucifer.treasure.enchant.effect;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.enchantment.EnchantmentEffectContext;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.EnchantmentLevelBasedValue;
import net.minecraft.enchantment.effect.EnchantmentEntityEffect;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;

public record StepEnchantmentEffect(EnchantmentLevelBasedValue amount) implements EnchantmentEntityEffect {
    public static final MapCodec<StepEnchantmentEffect> CODEC = RecordCodecBuilder.mapCodec(instance ->
            instance.group(
                    EnchantmentLevelBasedValue.CODEC.fieldOf("amount").forGetter(StepEnchantmentEffect::amount)
            ).apply(instance, StepEnchantmentEffect::new)
    );

    private static final String NBT_STEP_ON = "lucifer_treasure_step_enabled";

    public static void initialize() {
        ServerTickEvents.END_SERVER_TICK.register(server -> {
            server.getPlayerManager().getPlayerList().forEach(player -> {
                ItemStack leggings = player.getEquippedStack(EquipmentSlot.LEGS);

                if (!leggings.isEmpty() && leggings.getItem() instanceof ArmorItem) {
                    var enchantments = EnchantmentHelper.getEnchantments(leggings);
                    boolean hasStepEnchant = false;

                    for (var entry : enchantments.getEnchantments()) {
                        if (entry.getKey().isPresent()) {
                            String enchantmentId = entry.getKey().get().getValue().toString();
                            if (enchantmentId.equals("lucifer_treasure:step")) {
                                hasStepEnchant = true;
                                break;
                            }
                        }
                    }

                    if (hasStepEnchant) {
                        enableStepHeight(player);
                    } else {
                        disableStepHeight(player);
                    }
                }
            });
        });
    }

    private static void enableStepHeight(PlayerEntity player) {
        NbtCompound nbt = player.writeNbt(new NbtCompound());
        if (!nbt.getBoolean(NBT_STEP_ON)) {
            nbt.putBoolean(NBT_STEP_ON, true);
            player.readNbt(nbt);

            // 设置步高属性
            if (player.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT) != null) {
                player.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT).setBaseValue(1.0);
            }
        }
    }

    private static void disableStepHeight(PlayerEntity player) {
        NbtCompound nbt = player.writeNbt(new NbtCompound());
        if (nbt.getBoolean(NBT_STEP_ON)) {
            nbt.putBoolean(NBT_STEP_ON, false);
            player.readNbt(nbt);

            // 重置步高属性
            if (player.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT) != null) {
                player.getAttributeInstance(EntityAttributes.GENERIC_STEP_HEIGHT).setBaseValue(0.6); // 默认值
            }
        }
    }

    @Override
    public void apply(ServerWorld world, int level, EnchantmentEffectContext context, Entity target, Vec3d pos) {
        // 逻辑在事件监听器中处理
    }

    @Override
    public MapCodec<? extends EnchantmentEntityEffect> getCodec() {
        return CODEC;
    }
} 